<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <!-- <a-button type="default" @click="handleImport()">
          <Icon icon="ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button> -->
        <!-- <a-button type="primary" @click="handleForm({})" v-auth="'shop:order:cusFpApply:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
        <a-button type="primary" @click="handleStatus()" v-auth="'shop:order:cusFpApply:edit'">
          <Icon icon="ant-design:history-outlined" /> {{ t('批量修改状态') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.cusCode }}
        </a>
      </template>

      <template #fpUrl="{ record }">
        <a @click="handleopen(record.fpUrl)">
          {{ record.fpUrl }}
        </a>
      </template>

      <!-- v-if="hasPermission(['shop:order:cusFpApply:edit'])" -->
      <template #uploadFile="{ record }">
        <BasicUpload
          v-model:value="record.dataMap"
          disabled
          :bizKey="record.id"
          :bizType="'cusFpApply_file'"
          :uploadType="'file'"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>

    <!-- 订单详情 -->
    <ListDetails @register="registerDrawerListDetails" @success="handleSuccess" />

    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />

    <!-- 状态 -->
    <BasicModal v-bind="$attrs" @register="registerModal" title="状态修改" @ok="handleModal">
      <BasicForm @register="registerForm" />
    </BasicModal>

    <!-- 链接 -->
    <BasicModal
      v-bind="$attrs"
      @register="registerModal1"
      title="上传链接"
      @ok="handleModal1"
      width="60%"
    >
      <BasicForm @register="registerForm1" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopOrderCusFpApplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    cusFpApplyDelete,
    cusFpApplyListData,
    updateStatus,
    uploadKpResult,
    clearKpResult,
  } from '/@/api/shop/order/cusFpApply';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal, BasicModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import ListDetails from './listDetails.vue';
  import { BasicUpload } from '/@/components/Upload';

  import FormImport from './formImport.vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';

  const { t } = useI18n('shop.order.cusFpApply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('开票申请管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('开票状态'),
        field: 'kpStatus',
        component: 'Select',
        componentProps: {
          dictType: 'cus_fp_status',
          allowClear: true,
        },
      },
      {
        label: t('客户名称'),
        field: 'basCus.name',
        component: 'Input',
      },
      // {
      //   label: t('客户编码'),
      //   field: 'cusCode',
      //   component: 'Input',
      // },
      {
        label: t('申请人'),
        field: 'creator.userName',
        component: 'Input',
      },
      {
        label: t('申请时间'),
        field: 'createDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('开票人'),
        field: 'overBy',
        component: 'Input',
      },
      {
        label: t('开票完成'),
        field: 'overDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
    ],
    fieldMapToTime: [
      ['createDate', ['createDate_gte', 'createDate_lte']],
      ['overDate', ['overDate_gte', 'overDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('开票状态'),
      dataIndex: 'kpStatus',
      key: 'a.kp_status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'cus_fp_status',
    },
    {
      title: t('发票附件'),
      dataIndex: 'uploadFile',
      width: 100,
      align: 'left',
      slot: 'uploadFile',
    },
    {
      title: t('客户编码'),
      dataIndex: 'cusCode',
      key: 'a.cus_code',
      sorter: true,
      width: 120,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('客户简称'),
      dataIndex: 'basCus.abbName',
      key: 'cus.abb_name',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('客户名称'),
      dataIndex: 'basCus.name',
      key: 'cus.name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('归属公司'),
      dataIndex: 'basCus.exDefine12',
      key: 'cus.ex_define12',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('申请金额'),
      dataIndex: 'isum',
      key: 'a.isum',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('申请人'),
      dataIndex: 'creator.userName',
      key: 'a.create_by',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 120,
      align: 'center',
    },

    {
      title: t('开票时间'),
      dataIndex: 'overDate',
      key: 'a.over_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('开票人'),
      dataIndex: 'overUser.userName',
      key: 'a.over_by',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('发票链接'),
      dataIndex: 'fpUrl',
      key: 'a.fp_url',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'fpUrl',
    },
    {
      title: t('抬头类型'),
      dataIndex: 'kpInfo.ctype',
      key: 'a.ctype',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'cus_fp_type',
    },
    {
      title: t('名称'),
      dataIndex: 'kpInfo.cname',
      key: 'a.cname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系方式'),
      dataIndex: 'kpInfo.contactInfo',
      key: 'a.contact_info',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('税号'),
      dataIndex: 'kpInfo.dutyNo',
      key: 'a.duty_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('开户银行'),
      dataIndex: 'kpInfo.bankName',
      key: 'a.bank_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('银行帐号'),
      dataIndex: 'kpInfo.bankCode',
      key: 'a.bank_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位号码'),
      dataIndex: 'kpInfo.dwNo',
      key: 'a.dw_no',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('单位地址'),
      dataIndex: 'kpInfo.dwAddr',
      key: 'a.dw_addr',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('取消说明'),
      dataIndex: 'cancleRemarks',
      key: 'a.cancle_remarks',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 180,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 400,
    actions: (record: Recordable) => [
      {
        // icon: 'ant-design:eye-outlined',
        // title: t('查看开票申请详情'),
        label: '查看详情',
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:order:cusFpApply:edit',
      },
      {
        // icon: 'ant-design:eye-outlined',
        // title: t('查看开票申请详情'),
        label: '查看订单详情',
        onClick: handleListDetails.bind(this, { id: record.id }),
        auth: 'shop:order:cusFpApply:edit',
      },
      {
        // icon: 'clarity:note-edit-line',
        // title: t('上传开票结果'),
        label: t('上传开票结果'),
        onClick: handleploadKp.bind(this, { id: record.id }),
        auth: 'shop:order:cusFpApply:edit',
      },
      {
        // icon: 'ant-design:delete-outlined',
        // title: t('清除开票结果'),
        color: 'error',
        label: t('清除开票结果'),
        // onClick: handleForm.bind(this, { id: record.id }),
        popConfirm: {
          title: t('是否确认清除开票结果'),
          confirm: handleclear.bind(this, { id: record.id }),
        },
        auth: 'shop:order:cusFpApply:edit',
      },
      // {
      //   icon: 'ant-design:delete-outlined',
      //   color: 'error',
      //   title: t('删除开票申请'),
      //   popConfirm: {
      //     title: t('是否确认删除开票申请'),
      //     confirm: handleDelete.bind(this, { id: record.id }),
      //   },
      //   auth: 'shop:order:cusFpApply:edit',
      //   // ifShow: record.auditStatus != '1',
      // },
    ],
  };

  const [registerForm, { resetFields, validate }] = useForm({
    labelWidth: 120,
    schemas: [
      {
        label: t('开票状态'),
        field: 'kpStatus',
        component: 'Select',
        componentProps: {
          dictType: 'cus_fp_status',
          allowClear: true,
        },
        required: true,
        colProps: { lg: 24, md: 24 },
        itemProps: { validateTrigger: 'blur' },
      },
      {
        label: t('取消原因'),
        field: 'cancleRemarks',
        component: 'InputTextArea',
        componentProps: {
          allowClear: true,
        },
        ifShow: (e) => {
          console.log(e, '22223333');
          return e.model.kpStatus == '9' ? true : false;
        },
        colProps: { lg: 24, md: 24 },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerForm1, { resetFields: resetFields1, validate: validate1 }] = useForm({
    labelWidth: 120,
    schemas: [
      {
        label: t('发票链接'),
        field: 'fpUrl',
        component: 'Input',
        componentProps: {
          maxlength: 100,
        },
      },
      {
        label: t('上传附件'),
        field: 'dataMap',
        component: 'Upload',
        componentProps: {
          loadTime: computed(() => fpdata.value.__t),
          bizKey: computed(() => fpdata.value.id),
          bizType: 'cusFpApply_file',
          uploadType: 'file',
          // accept:'.pdf'
          // allowSuffixes:'.pdf'
        },
        colProps: { lg: 24, md: 24 },
      },
    ],
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawerListDetails, { openDrawer: openDrawerListDetails }] = useDrawer();
  const [registerModal1, { openModal: openModal1, closeModal: closeModal1 }] = useModal();
  const [registerModal, { openModal, closeModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: cusFpApplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleopen(fpUrl) {
    window.open(fpUrl, '_blank');
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handleListDetails(record: Recordable) {
    openDrawerListDetails(true, record);
  }

  let fpdata = ref<any>({});
  async function handleploadKp(record: Recordable) {
    fpdata.value = record;
    fpdata.value.__t = new Date().getTime();
    await openModal1(true, {});
    resetFields1();
  }

  async function handleModal1() {
    let data = await validate1();
    data.id = fpdata.value.id;
    const res = await uploadKpResult(data);
    showMessage(res.message);
    setTimeout(closeModal1);
    handleSuccess();
  }

  async function handleclear(record: Recordable) {
    const res = await clearKpResult(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/shop/order/cusFpApply/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleStatus() {
    if (getSelectRowKeys().length) {
      await openModal(true, {});
      resetFields();
    } else {
      showMessage('请至少选择一条数据');
    }
  }

  async function handleModal() {
    let ids = await getSelectRowKeys();
    let data = await validate();
    data.selIds = ids.join(',');
    const res = await updateStatus(data);
    showMessage(res.message);
    setTimeout(closeModal);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await cusFpApplyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
